package com.member.web.controller.shop;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.member.common.annotation.Log;
import com.member.common.core.controller.BaseController;
import com.member.common.core.domain.AjaxResult;
import com.member.common.core.page.TableDataInfo;
import com.member.common.enums.BusinessType;
import com.member.common.exception.ServiceException;
import com.member.common.utils.poi.ExcelUtil;
import com.member.shop.domain.SysBizCouponGoods;
import com.member.shop.domain.SysBizCouponGrantUser;
import com.member.shop.dto.SysBizCouponGrantUserDto;
import com.member.shop.service.ISysBizCouponGrantUserService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * 【优惠券发放用户】控制器
 *
 * @author lixinchen
 * @version 1.0
 * @date 2024-05-08
 **/
@RestController
@RequestMapping("/shop/couponGrantUser")
public class SysBizCouponGrantUserController extends BaseController {
    @Resource
    private ISysBizCouponGrantUserService sysBizCouponGrantUserService;

    private void setTenantId(SysBizCouponGrantUser sysBizCouponGrantUser) {
        Long tenantId = getTenantId();
        if (tenantId != null && tenantId > 0) {
            sysBizCouponGrantUser.setTenantId(tenantId);
        }
    }

    /**
     * 查询优惠券发放用户列表
     */
    @PreAuthorize("@ss.hasPermi('shop:couponGrantUser:list')")
    @GetMapping("/list")
    public TableDataInfo list(SysBizCouponGrantUser sysBizCouponGrantUser) {
        startPage();
        setTenantId(sysBizCouponGrantUser);
        List<SysBizCouponGrantUser> list = sysBizCouponGrantUserService.selectSysBizCouponGrantUserList(sysBizCouponGrantUser);
        return getDataTable(list);
    }

    /**
     * 导出优惠券发放用户列表
     */
    @PreAuthorize("@ss.hasPermi('shop:couponGrantUser:export')")
    @Log(title = "优惠券发放用户", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SysBizCouponGrantUser sysBizCouponGrantUser) {
        setTenantId(sysBizCouponGrantUser);
//        LambdaQueryWrapper<SysBizCouponGrantUser> queryWrapper = new LambdaQueryWrapper<SysBizCouponGrantUser>();
//        queryWrapper.eq(SysBizCouponGrantUser::getGrantId, sysBizCouponGrantUser.getGrantId());
//        List<SysBizCouponGrantUser> list = sysBizCouponGrantUserService.selectSysBizCouponGrantUserList(queryWrapper);
        List<SysBizCouponGrantUser> list = sysBizCouponGrantUserService.selectSysBizCouponGrantUserList(sysBizCouponGrantUser);
        ExcelUtil<SysBizCouponGrantUser> util = new ExcelUtil<SysBizCouponGrantUser>(SysBizCouponGrantUser.class);
        util.exportExcel(response, list, "优惠券发放用户数据");
    }

    /**
     * 导出优惠券发放用户列表 模板
     */
    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response) {
        ExcelUtil<SysBizCouponGrantUserDto> util = new ExcelUtil<SysBizCouponGrantUserDto>(SysBizCouponGrantUserDto.class);
        util.importTemplateExcel(response, "优惠券发放用户模板");
    }

    /**
     * 导出优惠券发放用户列表 模板
     */
    @PostMapping("/importUser")
    public List<String> importData(MultipartFile file, boolean updateSupport) throws Exception {
        ExcelUtil<SysBizCouponGrantUserDto> util = new ExcelUtil<SysBizCouponGrantUserDto>(SysBizCouponGrantUserDto.class);
        List<SysBizCouponGrantUserDto> sysBizCouponGrantUser = util.importExcel(file.getInputStream());
        if (CollectionUtils.isEmpty(sysBizCouponGrantUser)) {
            throw new ServiceException("导入数据不能为空");
        }
        sysBizCouponGrantUser = sysBizCouponGrantUser.parallelStream().filter(Objects::nonNull).collect(Collectors.toList());
        if (CollectionUtils.isEmpty(sysBizCouponGrantUser)) {
            throw new ServiceException("模板不正确");
        }
        return sysBizCouponGrantUser.stream().map(SysBizCouponGrantUserDto::getPhone).collect(Collectors.toList());
    }

    /**
     * 获取优惠券发放用户详细信息
     */
    @PreAuthorize("@ss.hasPermi('shop:couponGrantUser:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(sysBizCouponGrantUserService.selectSysBizCouponGrantUserById(id));
    }

    /**
     * 新增优惠券发放用户
     */
    @PreAuthorize("@ss.hasPermi('shop:couponGrantUser:add')")
    @Log(title = "优惠券发放用户", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SysBizCouponGrantUser sysBizCouponGrantUser) {
        return toAjax(sysBizCouponGrantUserService.insertSysBizCouponGrantUser(sysBizCouponGrantUser));
    }

    /**
     * 修改优惠券发放用户
     */
    @PreAuthorize("@ss.hasPermi('shop:couponGrantUser:edit')")
    @Log(title = "优惠券发放用户", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SysBizCouponGrantUser sysBizCouponGrantUser) {
        return toAjax(sysBizCouponGrantUserService.updateSysBizCouponGrantUser(sysBizCouponGrantUser));
    }

    /**
     * 删除优惠券发放用户
     */
    @PreAuthorize("@ss.hasPermi('shop:couponGrantUser:remove')")
    @Log(title = "优惠券发放用户", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(sysBizCouponGrantUserService.deleteSysBizCouponGrantUserByIds(ids));
    }
}
